import 'package:dio/dio.dart';
import 'package:myapp/repository/datas/common_website_data.dart';
import 'package:myapp/repository/datas/home_baner_data.dart';
import 'package:myapp/repository/datas/home_list_data.dart';
import 'package:myapp/repository/datas/knowledge_detail_list_data.dart';
import 'package:myapp/repository/datas/my_collects_model.dart';
import 'package:myapp/repository/datas/search_hot_keys_data.dart';

import '../http/dio_instance.dart';
import 'datas/app_check_update_model.dart';
import 'datas/auth_data.dart';
import 'datas/knowledge_list_data.dart';
import 'datas/search_data.dart';

class Api {
  static Api instance = Api._();

  Api._();

  //获取首页数据
  Future<List<HomeBannerData>?> getBanner() async {
    Response response = await DioInstance.instance().get(path: "/banner/json");
    HomeBannerListData bannerData = HomeBannerListData.fromJson(response.data);
    return bannerData.bannerList;
  }

  //获取首页文章数据
  Future<List<HomeListItemData>?> getHomeList(String pageCount) async {
    Response response = await DioInstance.instance().get(
      path: "/article/list/$pageCount/json",
    );
    HomeListData homeListData = HomeListData.fromJson(response.data);
    return homeListData.datas;
  }

  //获取首页置顶文章数据
  Future<List<HomeListItemData>?> getHomeTopList() async {
    Response response = await DioInstance.instance().get(
      path: "/article/top/json",
    );
    HomeTopListData homeListData = HomeTopListData.fromJson(response.data);
    return homeListData.topList;
  }

  //获取常用网站数据
  Future<List<CommonWebsiteData>?> getWebsiteList() async {
    Response response = await DioInstance.instance().get(path: "friend/json");
    CommonWebsiteListData websiteListData = CommonWebsiteListData.fromJson(
      response.data,
    );
    return websiteListData.websiteList;
  }

  //获取搜索热点数据
  Future<List<SearchHotKeysData>?> getSearchHotKyes() async {
    Response response = await DioInstance.instance().get(path: "hotkey/json");
    SearchHotKeysListData hotKeysListData = SearchHotKeysListData.fromJson(
      response.data,
    );
    return hotKeysListData.websiteList;
  }

  //登录
  Future<AuthData> login({String? name,String? password}) async {
    Response response = await DioInstance.instance().post(path: "user/login", queryParameters: {
      "username": name,
      "password": password,
    });
    return AuthData.fromJson(response.data);
  }

  //获取搜索热点数据
  Future<dynamic> register({
    String? name,
    String? password,
    String? rePassword,
  }) async {
    Response response = await DioInstance.instance().post(
      path: "user/register",
      queryParameters: {
        "username": name,
        "password": password,
        "repassword": rePassword,
      });
    return true;
  }

  ///获取体系列表
  Future<List<KnowledgeListData?>?> knowledgeList() async{
    Response response = await DioInstance.instance().get(path: "tree/json");
    KnowledgeData knowledgeData = KnowledgeData.fromjson(response.data);
    return knowledgeData.list;
  }

  ///收藏 例子：lg/collect/1065/json
  Future<bool?> collect(String? id) async{
    Response response = await DioInstance.instance().post(path: "lg/collect/$id/json");
    return boolCallback(response.data);
  }

  Future<bool?> unCollect(String? id) async{
    Response response = await DioInstance.instance().post(path: "lg/uncollect_originId/$id/json");
    return boolCallback(response.data);
  }

  //user/logout/json
  //退出登录
  Future<bool?> logout() async{
    Response response = await DioInstance.instance().get(path: "user/logout/json");
    return boolCallback(response.data);
  }

  //获取体系明细数据
  Future<List<KnowledgeDetailItemData>?> detailKnowledgeList(String? pageCount,String? cid) async{
    Response response = await DioInstance.instance().get(path: "article/list/$pageCount/json",param: {"cid":cid});
    var knowledgeDetailListData = KnowledgeDetailListData.fromJson(response.data);
    return knowledgeDetailListData.datas;
  }


  //搜索  https://www.wanandroid.com/article/query/0/json
  Future<List<SearchListData>?> searchList({String? pageCount,String? keyword}) async{
    Response response = await DioInstance.instance().post(path: "article/query/${pageCount}/json",queryParameters: {"k":keyword});
    var searchListData = SearchData.fromJson(response.data);
    return searchListData.datas;
  }

  ///获取我的收藏列表
  Future<List<MyCollectItemModel>?> getMyCollects(String pageCount) async{
    Response rsp = await DioInstance.instance().get(path: "lg/collect/list/$pageCount/json");
    MyCollectsModel? model = MyCollectsModel.fromJson(rsp.data);
    if (model.datas != null && model.datas?.isNotEmpty == true) {
      return model.datas;
    }
    return [];
  }

  ///检查app新版本
  Future<AppCheckUpdateModel?> checkUpdate() async {
    DioInstance.instance().changeBaseUrl("https://www.pgyer.com/");
    Response response = await DioInstance.instance().post(
        path: "apiv2/app/check",
        queryParameters: {
          "_api_key": "57c543d258a34f8565748561de50b6e6",//蒲公英平台查看
          "appKey": "2639f784ce9ee850532074b7b0534e62"//蒲公英上传app后会生成一个
        });

    DioInstance.instance().changeBaseUrl("https://www.wanandroid.com/");

    return AppCheckUpdateModel.fromJson(response.data);
  }


  bool? boolCallback(dynamic data){
    if (data !=null && data is bool){
      return data;
    }
    return false;
  }

}
